﻿using System;
using System.Xml.Linq;
using TVersityDotNet.HelperTypes;

namespace TVersityDotNet.Responses
{
    public class SettingsFetchResponse : TVersityResponse
    {
        public TVSettings Settings { get; set; }

        //TODO: remove calls to virtuals from within constructors (bug: 28027)
        public SettingsFetchResponse(XDocument xmlDocument)
        {
            this.Settings = new TVSettings();
            if (xmlDocument != null)
                this.ParseMessage(xmlDocument);
            else
            {
                this.ResponseSuccess = false;
                this.ResponseMessage = "Incomming xmlDocument was null";
            }
        }

        public override void ParseMessage(XDocument responseXml)
        {
            if (responseXml != null)
            {
                base.ParseMessage(responseXml);

                var element = responseXml.Descendants("result");
                foreach (XAttribute attr in element.Attributes())
                {
                    this.Settings.PropertyBag.AddPropertyToPropertyBag(attr.Name.LocalName.ToLowerInvariant(), attr.Value);
                }
                ConnectionSettingsContainer container = new ConnectionSettingsContainer(this.Settings.PropertyBag.GetPropertyFromPropertyBag("ConnectionSpeed".ToLowerInvariant()));
                this.Settings.ConnectionQuality = container.Quality;
                this.Settings.ConnectionSpeed = container.Speed;
            }
            else
            {
                this.ResponseSuccess = false;
                this.ResponseMessage = "Provided responseXml was null";
            }

        
        }
    }

    
}
